﻿using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Configuration;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web.UI
{
    public partial class UserPage : BasePage
    {
        protected Model.users userModel;
        protected Model.user_groups groupModel;

        /// <summary>
        /// 重写父类的虚方法,此方法将在Init事件前执行
        /// </summary>
        protected override void ShowPage()
        {
            this.Init += new EventHandler(UserPage_Init); //加入IInit事件
        }

        protected void JscriptMsg(string msgtitle, string url, string msgcss)
        {
            string script = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\", \"" + msgcss + "\")";
            base.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "JsPrint", script, true);
        }

        protected void JscriptMsg(string msgtitle, string url, string msgcss, string callback)
        {
            string script = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\", \"" + msgcss + "\", " + callback + ")";
            base.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "JsPrint", script, true);
        }
        /// <summary>
        /// OnInit事件,检查用户是否登录
        /// </summary>
        void UserPage_Init(object sender, EventArgs e)
        {
            this.InitPage();
            string str = MyCommFun.RequestOpenid();
            if (!IsUserLogin())
            {
                //跳转URL
              //  HttpContext.Current.Response.Redirect(linkurl("login"));
                if (string.IsNullOrEmpty(str))
                {
                    base.Response.Redirect(string.Concat(new object[] { "/wxoauth.aspx?wid=", this.wid, "&p=9&ReturnURL=", base.Server.UrlEncode(base.Request.RawUrl) }));
                    return;
                }
                this.openid = str;
            }
            //获得登录用户信息
            userModel = new BLL.users().GetModelByOpenid(this.openid);
            if (userModel == null)
            {
                userModel = GetUserInfo();
            }
            
            if (MyCommFun.RequestOpenid() != this.userModel.wxOpenId)
            {
                this.openid = this.userModel.wxOpenId;
            }
            groupModel = new BLL.user_groups().GetModel(userModel.group_id);
            if (groupModel == null)
            {
                groupModel = new Model.user_groups();
            }
        }

        /// <summary>
        /// 构建一个虚方法，供子类重写
        /// </summary>
        protected virtual void InitPage()
        {
            this.serverPath = "";
            this.templateFileName = "";
            this.errInitTemplates = "";
            this.wid = MyCommFun.RequestInt("wid");
            if (this.wid == 0)
            {
                this.errInitTemplates = "链接地址或者参数错误！";
            }
        }


        public string errInitTemplates { get; set; }

        public string openid { get; set; }

        public string serverPath { get; set; }

        public string templateFileName { get; set; }

        public string tPath { get; set; }

        public int wid { get; set; }
        public int shopid { get; set; }
    }
}